package com.lgy.service.impl;

import com.lgy.dto.OrderDTO;
import com.lgy.service.PushMessage;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
@Slf4j
public class PushMessageImpl implements PushMessage {

    @Autowired
    private WxMpService wxMpService;

    @Override
    public void orderStatus(OrderDTO orderDTO) {
        WxMpTemplateMessage templateMessage = new WxMpTemplateMessage();
        templateMessage.setTemplateId("模板id");
        templateMessage.setToUser("推送的openid");
        List<WxMpTemplateData> data = Arrays.asList(
                new WxMpTemplateData("first", "亲, 请记得收获"),
                new WxMpTemplateData("keyword1", "微信点餐"), // 商家名称
                new WxMpTemplateData("keyword2", "xxx"), // 商家电话
                new WxMpTemplateData("keyword3", orderDTO.getOrderId()), // 订单号
                new WxMpTemplateData("keyword4", orderDTO.getOrderStatusEnum().getMessage()), // 状态
                new WxMpTemplateData("keyword5", "￥" + orderDTO.getOrderAmount()), // 总价
                new WxMpTemplateData("remark", "欢迎再次光临") // 备注
        );
        templateMessage.setData(data);
        try {
            wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
        } catch (WxErrorException e) {
            log.error("消息推送发送失败");
        }
    }
}
